package com.pactera.madp.cp.controller.sidebar.employee;

import com.pactera.madp.common.core.util.R;
import com.pactera.madp.common.log.annotation.SysLog;
import com.pactera.madp.cp.api.dto.emp.EmployeeTagChangeDTO;
import com.pactera.madp.cp.api.dto.emp.EmployeeTagCreateDTO;
import com.pactera.madp.cp.api.vo.EmployeeTagVO;
import com.pactera.madp.cp.service.EmployeeTagService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @author:  yushen.qi
 * @time: 2020/12/4 2:39 下午
 * @description 企业成员
 */
@RestController
@AllArgsConstructor
@RequestMapping("/employeewechat")
@Api(value = "employee", tags = "个人标签管理-侧边栏")
@Slf4j
public class EmployeeTagSidebarController {

    @Resource
    private EmployeeTagService employeeTagService;

    /**
     * 添加个人新标签
     *
     * @param dto
     * @return
     */
    @SysLog("添加个人新标签")
    @PostMapping
    @ApiOperation(value = "添加个人新标签")
    public R<Map<Object, Object>> insert(@RequestBody EmployeeTagCreateDTO dto){
        log.info("dto"+dto.getTagName());
        log.info("dto"+dto.getWxExternalUserid());
        log.info("dto"+dto.getWxUserId());
        Integer distinct = employeeTagService.isDistinct(dto.getWxExternalUserid(), dto.getWxUserId(), dto.getTagName());
        if(distinct>0){
            return R.failed("标签名重复！");
        }
        return R.ok(employeeTagService.insert(dto));
    }

    /**
     * 查询个人标签
     *
     * @param wxExternalUserid
     * @param wxUserId
     * @return
     */
    @GetMapping("/index")
    @ApiOperation(value = "查询个人标签")
    public R<List<EmployeeTagVO>> list(@RequestParam(value = "wxExternalUserid") String wxExternalUserid, @RequestParam(value = "wxUserId") String wxUserId ){
        return R.ok(employeeTagService.list(wxExternalUserid,wxUserId));
    }

    /**
     * 删除个人标签
     *
     * @param id
     * @return
     */
    @SysLog("删除个人标签")
    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除个人标签")
    public R<Boolean> remove(@PathVariable Integer id){
      return employeeTagService.delete(id)?R.ok():R.failed("删除失败");
    }

    /**
     * 选择标签展示
     *
     * @param dto
     * @return
     */
    @SysLog("选择标签展示")
    @PostMapping("/change")
    @ApiOperation(value = "选择标签展示")
    public  R changeState (@RequestBody EmployeeTagChangeDTO dto){
    employeeTagService.changeState(dto);
        return R.ok();
    }

    /**
     * 查询展示标签
     *
     * @param wxExternalUserid
     * @param wxUserId
     * @return
     */
    @GetMapping("/state")
    @ApiOperation(value = "查询展示标签")
    public R stateList(@RequestParam(value = "wxExternalUserid") String wxExternalUserid, @RequestParam(value = "wxUserId") String wxUserId){
     return R.ok(employeeTagService.stateList(wxExternalUserid,wxUserId));
    }

    @SysLog("展示标签更改")
    @GetMapping("/del")
    @ApiOperation(value = "展示标签更改")
    public R changeState(@RequestParam(value = "id") Integer id){
     return employeeTagService.changeStateById(id)?R.ok():R.failed("清除失败");
    }

}
